import os
import random
import time

import requests

from lxml import etree


# 爬取前 10页照片
def get_picture(size = 10):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36",
        "Cookie": "cf_clearance=5ki4tgH29he9JM8VtQS5uBZ8bGp3wGsr7aUcye39UCM-1739284297-1.2.1.1-eEVvuwIiwUScZUmnCI7sWUiB6cDSj03QnO1evEnuzc64.tULjIzkDTSAsBHgPl1kjtkTjNd2_ehjIKyDJcxZs5Thqf2djRdCg9wqah3ewTpdFYV2ENA8yIDauPL6bCfrIaCrjrtWDhXAD8etj0qk_fycs9Pr.kBrpgOOFWOWhHv8pev3Z_AvaAx0CJeOe1I8xsdUebI2WHBhWmSSMBoaON18MgvhgaQc84IkwDCLukeuU9VkJKcNeBg16HZDTIS0c7Ouizv.CMSx.epqoI1tUeuQ.xJ1w3OcO7cj9Ox17GAmB4pn9i2eD.8EeY9dCElEro_KkzLPd1dgsRP2GnUP0g"
    }
    for i in range(size):
        print(f"正在爬取第{i+1}页")
        if i == 0:
            url = "https://pic.netbian.com/"
        else:
            url = f"https://pic.netbian.com/index_{i+1}.html"
        res = requests.get(url, headers=headers)
        print(res.content.decode("GBK"))
        tree = etree.HTML(res.content.decode("GBK"))
        img_names = tree.xpath('//ul[@class="clearfix"]/li/a/span/img/@alt')
        img_urls = tree.xpath('//ul[@class="clearfix"]/li/a/span/img/@src')
        time.sleep(random.randint(1, 4))
        # 下载图片
        for img_url, img_name in zip(img_urls, img_names):
            download_src = url + img_url
            picture_res = requests.get(download_src, headers=headers)
            # 创建文件夹
            if not os.path.exists("./img"):
                os.mkdir("./img")
            with open(f"./img/{img_name}", "wb") as f:
                f.write(picture_res.content)
            time.sleep(random.randint(1, 4))

if __name__ == '__main__':
    get_picture()